# -*- coding:utf-8 -*-
import os
import socket
import json
import httplib
import urllib2
import urllib
import uuid
import zipfile
import base64
import sys
import hashlib
import datetime
import time
import subprocess
import xml.dom.minidom
import shutil
import MySQLdb


def commom_log(filename, content , write_type='wb'):
    try:
        f = open(filename , write_type)
        f.write(content);
        f.close()
    except:
        print('happen an exception when write file')
        return ""

def get_version(random_filename):
    try:
        zf = zipfile.ZipFile(random_filename, "a")
        zf.getinfo("iTunesMetadata.plist")
        data = zf.read("iTunesMetadata.plist")
        if data :
            index   = data.rfind('bundleShortVersionString')
            if index != -1:
                index_right = data.find('</string>', index)
                version = data[index: index_right]
                version = version.replace("bundleShortVersionString</key>" , '').replace("<string>", "").strip()
            else:
                version = ''
            index   = data.rfind('bundleVersion')
            if index != -1:
                index_right = data.find('</string>', index)
                bundleVersion = data[index: index_right]
                bundleVersion = bundleVersion.replace("bundleVersion</key>" , '').replace("<string>", "").strip()
            else:
                bundleVersion = ''

            index   = data.rfind('itemId')
            if index != -1:
                index_right = data.find('</integer>', index)
                itunesid = data[index: index_right]
                itunesid = itunesid.replace("itemId</key>" , '').replace("<integer>", "").strip()
            else:
                itunesid = ''
            
            return  version,bundleVersion,itunesid
        else:
            return '','',''
    except:
        return '','',''




db_host   = "127.0.0.1"
db_user   = 'root'
db_passwd = ''
db_port   = 3306
db_name   = "app_xyapplist"


conn = MySQLdb.connect(host=db_host,user=db_user,passwd=db_passwd,port=db_port,init_command="set names utf8")
conn.select_db('app_xyapplist')
cur  = conn.cursor()
count = 0

sql  = "select itunesid from t_xyapplist_bian_2014_03_08_down where status = 4";
cur.execute(sql)
result = cur.fetchall()
for f in result:
    itunesid = f[0]

    m = hashlib.md5(str(itunesid)) 
    m.digest()
    md5  = m.hexdigest()
    str1 = md5[0: 2]
    str2 = md5[2: 4]

    dirpath1 = 'E:/handleSuc/'
    dirpath1_1 = 'E:/crackFail/crackFail/'

    dirpath2 = 'G:/handleSuc/'
    dirpath2_2 = 'G:/crackFail/crackFail/'

    dirpath3 = 'H:/handleSuc/'
    dirpath3_3 = 'H:/crackFail/crackFail/'

    dirpath4 = 'I:/handleSuc/'
    dirpath4_4 = 'I:/crackFail/crackFail/'

    random_filename =  str(md5) + str2 + str1 + '.ipa'
    is_has_ipas = 0
    if os.path.exists(dirpath1 + random_filename):
      is_has_ipas = 1

    if os.path.exists(dirpath1_1 + random_filename):
      is_has_ipas = 1

    
    if os.path.exists(dirpath2 + random_filename):
      is_has_ipas = 1

    if os.path.exists(dirpath2_2 + random_filename):
      is_has_ipas = 1
        

    if os.path.exists(dirpath3 + random_filename):
      is_has_ipas = 1

    if os.path.exists(dirpath3_3 + random_filename):
      is_has_ipas = 1

    if os.path.exists(dirpath4 + random_filename):
      is_has_ipas = 1

    if os.path.exists(dirpath4_4 + random_filename):
      is_has_ipas = 1

    if is_has_ipas == 1:
      str_sql = "update t_xyapplist_bian_2014_03_08_down set status=4 where itunesid="+str(itunesid)+" limit 1;"
      print str_sql
      commom_log('sql_log.txt', str_sql+"\n" , write_type='a')

        
cur.close()
conn.close()



# db_host   = "192.168.11.6"
# db_user   = 'xyzs'
# db_passwd = 'ikF5NNeJg2G8'
# db_port   = 3309
# db_name   = "app_xyapplist"


# conn = MySQLdb.connect(host=db_host,user=db_user,passwd=db_passwd,port=db_port,init_command="set names utf8")
# conn.select_db('app_xyapplist')
# cur  = conn.cursor()
# count = 0
# tb_name = 't_xyappsum'

# sql  = "select itunesid from t_xyappsum where is_j_install = 1 and is_hot = 1 and classid not in(13007,13006,13008,13009,13010,13011,13012,13013,13014,13015,13002,13017,13018,13003,13019,13020,13021,13001,13004,13023,13024,13025,13026,13027,13005,13028,13029,13030)";
# cur.execute(sql)
# result = cur.fetchall()
# for f in result:
#     itunesid = f[0]

#     m = hashlib.md5(str(itunesid)) 
#     m.digest()
#     md5  = m.hexdigest()
#     str1 = md5[0: 2]
#     str2 = md5[2: 4]

#     random_filename = '/data/app/jailbreak/'+ str(str1) + "/" + str(str2) + "/" + str(itunesid) + ".ipa"

#     if os.path.exists(random_filename):
#         random_filename = 'http://download.xyzs.com/jailbreak/' + str(str1) + "/" + str(str2) + "/" + str(itunesid) + ".ipa"
#         print random_filename + "============="
#         commom_log('download_log.log' , random_filename + "\n" , "a")
#         commom_log('itunesid_log.log' , str(itunesid) + "\n" , "a")
        
# cur.close()
# conn.close()


# private_sign = "%$@#%$#%^$#%$#";

# root_path = os.getcwd()
# root_path = root_path.replace("\\" , "/")

# rsync_path = root_path
# rsync_path = rsync_path.replace(":" , "")

# src_path  = root_path+"/ipas_e/"

# listfile=os.listdir(src_path)
# if len(listfile) > 0:
#    for line in listfile:
#        if line.rfind('.ipa') != -1:
#            return_value = get_version(unicode( src_path+line , "GBK" ))
#            version_j    = return_value[0]
#            bundleVersion_j = return_value[1]
#            itunesid     = return_value[2]
#            if itunesid != '':

#                m = hashlib.md5(str(itunesid)) 
#                m.digest()
#                md5_str = m.hexdigest()
#                xxxx = md5_str[0:1]
#                str1 = md5_str[0: 2]
#                str2 = md5_str[2: 4]

               
#                print "update app_xyapplist_"+str(xxxx)+".t_xyapplist_" + str(str1) + " set is_s_install=0 where itunesid="+str(itunesid)+" limit 1;"

#                print "update app_xyapplist.t_xyappsum set is_s_install=0 where itunesid="+str(itunesid)+" limit 1;"

#            else:
#                # 读取文件失败
#                print "itunesid is null\n"
#    else:
#        print "wait 10 min ..."
#        time.sleep(10)